<?php

class TestsController extends AppController
{
	public $name = 'Tests';
	public $helpers = array();
	public $uses = array('Test');
	protected $modelName = 'Test';
	protected $po_domain = '';
	protected $url_part  = 'test';

	function beforeFilter()
	{
		parent::beforeFilter();

		$this->modelName = 'Test';
		$this->po_domain = Inflector::underscore($this->name);
		$this->url_part  = 'tools';
		$this->set('po_domain', $this->po_domain);
		$this->set('url_part',  $this->url_part);
		$this->set('modelName', $this->modelName);
		$this->set('PKName',    $this->{$this->modelName}->primaryKey);
	    Auth::Access('active');
	}

	function tools_and_reports_list()
	{
		vendor('date');

	    $modelName = $this->modelName;
		$PKName    = $this->{$modelName}->primaryKey;
		$po_domain = $this->po_domain;
		$url_part  = $this->url_part;

		$this->pageTitle = strip_tags(__d($po_domain, 'prices_title', true));

		$this->{$modelName}->rel();
		$this->set('tests_list', $t = $this->{$modelName}->findAll(null, null, array("`{$modelName}`.`type` ASC")));
		$this->{$modelName}->Reports->rel();
		$this->set('reports_list', $t = $this->{$modelName}->Reports->findAll(null, null, array("`Report`.`type` ASC")));
	}

	function change_prices($id = null)
    {
	    Auth::Access('admin');
		$modelName = $this->modelName;
		$PKName    = $this->{$modelName}->primaryKey;
		$po_domain = $this->po_domain;
		$url_part  = $this->url_part;

		$this->pageTitle = strip_tags(__d($po_domain, 'details_title', true));

	    if (isset($this->params['id'])) {
	        $id = $this->params['id'];
	    }

	    $this->{$modelName}->rel();
        $tmp = $this->{$modelName}->read(null, $id);

		if (! is_array($tmp) || !count($tmp)) {
			$this->msgError(__d($po_domain, 'absent', true), @$error_code);
			$this->redirect("/{$url_part}/", null, true);
			exit();
		}

		////// MENU ///////////////////////

	    $ancestors_menu =& $this->MainMenu->findItem("/{$url_part}/");
	    $this->MainMenu->setActive($ancestors_menu);

		///////////////////////////////////

		$submit = !empty($this->data);
		if (! $this->{$modelName}->allowUpdate($tmp[$modelName])) {
		    $this->data = array($modelName => array());
		}
		foreach ($tmp as $_modelName => $_modelData) {
            if ($_modelName !== $modelName) {
    			$this->data[$_modelName] = $tmp[$_modelName];
            } else {
        		$this->data[$modelName] = am($tmp[$modelName], $this->data[$modelName]);
            }
		}

		if ($this->{$modelName}->allowUpdate($tmp[$modelName]) && $submit) {
			$error_msg = '';
			$success_msg = __d($po_domain, 'changed', true);
            if (empty($error_msg)) {
				if ($this->{$modelName}->save($this->data)) {

				} elseif (!empty($this->{$modelName}->validationErrors)) {
					$error_msg = __d(null, 'Data are incorrectly entered.', true).' '.__d(null, 'Try again.', true);
				} else {
	    		    $error_msg = __d(null, 'There was an internal error.', true).' '.__d(null, 'Try later.', true);
				}
            }
			if (empty($error_msg)) {
			    $this->msgNotice($success_msg);
			    $this->redirect("/{$url_part}/", null, true);
			    exit();
			} else {
			    $this->msgError($error_msg, @$error_code);
			}
		}

		$this->data[$modelName]['__allows']['update'] = $this->{$modelName}->allowUpdate($tmp[$modelName]);
		$this->data[$modelName]['__allows']['delete'] = $this->{$modelName}->allowDelete($tmp[$modelName]);
    }
}

?>